#include "board.h"
#include "control.h"
#include "element.h"
#include "imu.h"
#include "led.h"
#include "dma.h"
#include "easy_ui.h"
#include "menu.h"
#include "page.h"
#include "zf_tim.h"

void While_Handle(void)
{
	// static uint16 Count = 0;

	//  DMA_printf("%f,%f\n",IMU.EulerAngle[yaw],IMU_CalcYawRef());
	// DMA_printf("%f,%f,%f,%f\n", IMU.Temp, ADC.BatVol, ADC.InfVol, Ctrl.IndErr);
	// DMA_JustFloat(IMU.EulerAngle, 3);
	// DMA_JustFloat(IMU.FiltAccel, 3);
	// DMA_JustFloat(IMU.RawAccel, 3);
	//  DMA_printf("%f,%f,%f\n",gyro_offset[x],gyro_offset[y],gyro_offset[z]);

	// if (Counter.Main >= 500)
	// { // 运行频率检测
	// 	DMA_printf("%ld,%d\n", Counter.Main, Count);
	// 	Count = 0;
	// 	Counter.Main = 0;
	// }
	// Count++;

	LED_SetColor(PINK);
	Ctrl_Process();
	Elem_E2PROM();

	if (Ctrl.Car == DISABLE && Counter.Main % 25 == 0 && (IMU.CaliGyro || IMU.CaliZero) == DISABLE)
	{
		if (Page.ElemSet == DISABLE)
		{
			Page_Process(); // 26
		}
		else
		{
			EasyUI(20); // 21
		}
	}
}

void main(void)
{
	// 主板和模块初始化
	Board_Init();
	// 控制算法初始化
	Ctrl_Init();
	// 没这句printf单片机会死机，血淋淋的教训，花了三个小时排查的
	// DMA_printf("Initialization complete!");
	// 等待IMU预热完成
	u8g2_DrawStr(&u8g2, 40, 68, "Preheat");
	u8g2_SendBuffer(&u8g2);

	while (IMU.Preheat == ENABLE)
	{
		// 开机预热IMU
		IMU_Preheat();
	}

	Menu_Init();
	Page_Init();

	ctimer_count_clean(CTIM0_P34);
	ctimer_count_clean(CTIM3_P04);
	Counter.Tim4 = 0;

	while (1)
	{
		if (Counter.MainSW == ENABLE)
		{
			Counter.MainSW = DISABLE;
			While_Handle();
		}
	}
}
